package com.lzy.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lzy.entity.SysUserEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author lyw
 * @Date 2023/2/1-上午10:23
 */

@Data
@ApiModel("返回的用户")
@AllArgsConstructor
@NoArgsConstructor
public class LoginUser implements UserDetails, Serializable {

  private static final long serialVersionUID = 1L;

  private Long id;

  /**
   * 过期时间
   */
  @ApiModelProperty("过期时间起始")
  private Long expireTime = System.currentTimeMillis();

  /**
   * 登录时间
   */
  @ApiModelProperty("登陆时间")
  private Long loginTime;

  /**
   * 用户信息
   */
  @ApiModelProperty("用户信息")
  private SysUserEntity user;

  /**
   * 登录IP地址
   */
  @ApiModelProperty("登录IP地址")
  private String ipaddr;

  /**
   * 登录地点
   */
  @ApiModelProperty("登录地点")
  private String loginLocation;

  /**
   * 浏览器类型
   */
  @ApiModelProperty("浏览器类型")
  private String browser;

  /**
   * 操作系统
   */
  @ApiModelProperty("操作系统")
  private String os;

  /**
   * 权限列表
   */
  @ApiModelProperty("权限列表")
  private Set<String> permissions;

  /**
   * 角色列表
   */
  @ApiModelProperty("角色列表")
  private Set<String> roles;

  public Set<String> getPermissions() {
    return permissions;
  }

  public LoginUser(Long userId, SysUserEntity sysUserEntity, Set<String> permissions) {
    this.id = userId;
    this.user = sysUserEntity;
    this.permissions = permissions;
  }

  public void setPermissions(Set<String> permissions) {
    this.permissions = permissions;
  }

  public Long getLoginTime() {
    return loginTime;
  }

  public void setLoginTime(Long loginTime) {
    this.loginTime = loginTime;
  }

  public Long getExpireTime() {
    return expireTime;
  }

  public void setExpireTime(Long expireTime) {
    this.expireTime = expireTime;
  }

  public String getOs() {
    return os;
  }

  public void setOs(String os) {
    this.os = os;
  }

  public String getIpaddr() {
    return ipaddr;
  }

  public void setIpaddr(String ipaddr) {
    this.ipaddr = ipaddr;
  }

  public String getLoginLocation() {
    return loginLocation;
  }

  public void setLoginLocation(String loginLocation) {
    this.loginLocation = loginLocation;
  }

  public String getBrowser() {
    return browser;
  }

  public void setBrowser(String browser) {
    this.browser = browser;
  }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return null;
  }

  @Override
  @JsonIgnore
  public String getPassword() {
    return this.user.getPassword();
  }

  @Override
  @JsonIgnore
  public String getUsername() {
    return this.user.getUserName();
  }

  @Override
  @JsonIgnore
  public boolean isAccountNonExpired() {
    return true;
  }

  @Override
  @JsonIgnore
  public boolean isAccountNonLocked() {
    return true;
  }

  @Override
  @JsonIgnore
  public boolean isCredentialsNonExpired() {
    return true;
  }

  @Override
  @JsonIgnore
  public boolean isEnabled() {
    return true;
  }

  public static long getSerialVersionUID() {
    return serialVersionUID;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public SysUserEntity getUser() {
    return user;
  }

  public void setUser(SysUserEntity user) {
    this.user = user;
  }
}
